--
-- Author: shunguo.chen
-- Date: 2019-05-23 14:57:32
--
-- SkillMorePanel 更多技能选择界面
--

local SkillRender = import(".renders.SkillRender")
local SkillLegendRender = import(".renders.SkillLegendRender")
local SkillUpBtnPanel = import(".SkillUpBtnPanel")

local ClassRef = app.mvc.createPanelClass(...)

local SKILL_NODE_NUM = 7
local CHANGE_DEFAULT_SKILL_LV = 5 -- 替换技能默认技能等级=5
local WITH_LEGEND_HEIGHT = 450 -- 有神器的panel高度

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.SkillMoreNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

function ClassRef:remove()
    self._curSkillDescText:release()
    self._nextSkillDescText:release()
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    display.setCascadeOpacityEnabled(self, true)
    display.setCascadeColorEnabled(self, true)

    local width, height = self._ui.Panel:getContentSize2()
    self._initPanelWidth = width
    self._initPanelHeight = height

    display.nodeRemoveForNotHit(self._ui.Panel, function()
        self:moveOut()
    end)

    -- 神器面板
    local legendPanel = self._ui.Panel_artifact
    self._legendNameText = legendPanel:getChildByName("name")
    self._legendLvText = legendPanel:findChild("lv/tlt")
    self._legendIcon = legendPanel:findChild("lv/icon")
    self._legendTip = legendPanel:getChildByName("tips") 
    legendPanel:findChild("lv/grade"):loadTexture(Res.Qulity_Icon_Bg[6])

    self._legendSkillRenders = {}
    local node, render
    for i=1,2 do
        node = legendPanel:getChildByName("skill_" .. i)
        render = SkillLegendRender.create(node)
        render:onBtnClick(function(sender)
            self:_setCurRender(sender)
        end)
        self._legendSkillRenders[i] = render
    end

    local skillPanel = self._ui.Panel_skill
    local skillRenders = {}
    for i=1,SKILL_NODE_NUM do
        node = skillPanel:getChildByName("skill_" .. i)
        render = SkillRender.create(node)
        render:setCanChange(false)
        render:setClickHandler(function(sender)
            self:_setCurRender(sender)
        end)
        skillRenders[i] = render
    end
    self._skillRenders = skillRenders

    local bomPanel = self._ui.Panel_bom
    self._selectRender = SkillRender.create(bomPanel:getChildByName("icon"))
    self._selectRender:setIgnoreRedPoint(true)
    self._selectRender:unShowNameAndLv()
    self._selectRender:setClickEnable(false)
    self._selectRender:setChooseed(true)

    self._detailListView = bomPanel:getChildByName("ListView")
    self._skillNameText = bomPanel:getChildByName("Text_name")
    self._curSkillDescText = bomPanel:findChild("ListView/Text")
    self._nextSkillDescText = bomPanel:findChild("ListView/Text_1")
    self._curSkillDescText:retain()
    self._nextSkillDescText:retain()

    self._chooseBtn1 = bomPanel:getChildByName("Button_choose")
    self._chooseBtn2 = bomPanel:getChildByName("Button_choose_0")
    self._chooseBtn1:addClickEventListener(function()
        executeFunc(self._onOperate, "skill_choose", self._curSkill, self._sourceSkillIdx, self._curIsLegend)
        self:moveOut()
    end)
    self._chooseBtn2:addClickEventListener(function()
        executeFunc(self._onOperate, "skill_choose", self._curSkill, self._sourceSkillIdx, self._curIsLegend)
        self:moveOut()
    end)

    self._upBtnPanel = SkillUpBtnPanel.create(bomPanel:getChildByName("Panel_up"))
    self._upBtnPanel:onBtnClick(function(skill)
        executeFunc(self._onOperate, "skill_lvup", skill)
        self:_playAction()
    end)
end

function ClassRef:getGuideNode(id)
    -- 技能切换（技能切换面板第五个技能）id = 502
    return self._skillRenders[5]:getChildByName("Panel")
end

function ClassRef:moveIn()
    self:setVisible(true)
end

function ClassRef:moveOut()
    self:setVisible(false)
end

-- 1 SkillView使用
-- 2 SkillChangeView使用
function ClassRef:setShowType(tp)
    self._showType = tp
    if tp == 2 then
        self._chooseBtn1:setVisible(false)
        self._upBtnPanel:setVisible(false)
        self._chooseBtn2:setVisible(true)
    end
end

-- 技能有更新，局部刷新
function ClassRef:updateSkill(skill)
    local curData = self._curRender:getData()
    if curData and curData:getTeam() == skill:getTeam() then
        self._curRender:setData(skill)
        self:_setCurRender(self._curRender)
    else
        for i,render in ipairs(self._skillRenders) do
            local renderData = render:getData()
            if renderData and renderData:getTeam() == skill:getTeam() then
                render:setData(skill)
                return
            end
        end

        if self._ui.Panel_artifact:isVisible() then
            for i,render in ipairs(self._legendSkillRenders) do
                if render:isVisible() then
                    local renderData = render:getData()
                    if renderData and renderData:getTeam() == skill:getTeam() then
                        render:setData(skill)
                        return
                    end
                end
            end
        end
    end
end

-- skills 供选择的skills
-- sourceSkill 待替换的技能
-- sourceSkillIdx 待替换的技能位置
-- legend 神器提供额外技能
-- specType 当前技能专精
function ClassRef:refresh(skills, sourceSkill, sourceSkillIdx, legend, specType)
    self._sourceSkill = sourceSkill
    self._sourceSkillIdx = sourceSkillIdx
    self._legend = legend
    self._specType = specType

    local curRender, skill

    -- 神器技能
    if legend and specType then
        self._ui.Panel_artifact:setVisible(true)
        local size = cc.size(self._initPanelWidth, WITH_LEGEND_HEIGHT)
        self._ui.Panel:setContentSize(size)
        self._ui.Panel_bg:setContentSize(size)
        ccui.Helper:doLayout(self._ui.Panel_bg)

        self._legendNameText:setString(legend:getName())
        self._legendLvText:setString(string.format("Lv.%s", legend:getLv()))
        self._legendIcon:loadTexture(legend:getShowFashionIcon())

        local tipStr, chooseSkills
        if legend:getSpecId() == specType:getId() then
            tipStr = L("LegendBasic3_skill_ui2")
            chooseSkills = {}
        else
            chooseSkills = legend:getChooseSkills()
            if #chooseSkills == 0 then
                tipStr = L("LegendBasic3_skill_ui1")
            end
        end

        self._legendTip:setVisible(tipStr ~= nil)
        if tipStr then
            self._legendTip:setString(tipStr)
        end

        for i,render in ipairs(self._legendSkillRenders) do
            skill = chooseSkills[i]
            render:setData(skill)
            if skill and skill:getTeam() == sourceSkill:getTeam() then
                curRender = render
            end
        end
    else
        self._ui.Panel_artifact:setVisible(false)
        local size = cc.size(self._initPanelWidth, self._initPanelHeight)
        self._ui.Panel:setContentSize(size)
        self._ui.Panel_bg:setContentSize(size)
        ccui.Helper:doLayout(self._ui.Panel_bg)
    end
    
    -- 普通技能
    for i,render in ipairs(self._skillRenders) do
        skill = skills[i]
        render:setData(skill)
        if skill:getTeam() == sourceSkill:getTeam() then
            curRender = render
        end

        -- showType = 2，技能等级统一显示5级
        if self._showType == 2 then
            render:setLevel(CHANGE_DEFAULT_SKILL_LV)
        end
    end

    curRender = curRender or self._skillRenders[1]
    self:_setCurRender(curRender)
end

-- 刷新text的size
function ClassRef:_refreshTextSize(text)
    local size = text:getContentSize()
    text:getVirtualRenderer():setDimensions( size.width, 0 )
    local afterSize = text:getVirtualRendererSize()
    text:setContentSize( cc.size( size.width, afterSize.height ) )
end

function ClassRef:_setCurRender(render)
    if self._curRender then
        self._curRender:setChooseed(false)
    end
    render:setChooseed(true)
    self._curRender = render

    local skill = render:getData()
    self._curSkill = skill
    self._curIsLegend = render:getRenderName() == SkillLegendRender:getRenderName()

    self:_refreshSkill()
end

function ClassRef:_refreshSkill()
    local skill = self._curSkill

    self._selectRender:setData(skill)

    local nameStr = skill:getName() .. string.format(" Lv.%s", skill:getSkillLv())
    self._skillNameText:setString(nameStr)

    self._detailListView:removeAllItems()
    self._curSkillDescText:setString(skill:getDesc())
    self._nextSkillDescText:setString(skill:getNextDesc())
    self:_refreshTextSize(self._curSkillDescText)
    self:_refreshTextSize(self._nextSkillDescText)
    self._detailListView:pushBackCustomItem(self._curSkillDescText)
    self._detailListView:pushBackCustomItem(self._nextSkillDescText)

    if self._showType == 1 then
        local isMaxLevel = skill:isMaxLevel()
        self._upBtnPanel:setVisible(not isMaxLevel)
        if not isMaxLevel then
            self._upBtnPanel:setData(skill)
        end

        local isSourceSkill = skill:getId() == self._sourceSkill:getId()
        self._chooseBtn1:setVisible(not isMaxLevel and not isSourceSkill)
        self._chooseBtn2:setVisible(isMaxLevel and not isSourceSkill)
    else
        self._chooseBtn2:setVisible(skill:getId() ~= self._sourceSkill:getId())
    end
end

function ClassRef:_playAction()
    local actionNode = self._actionNode
    local actionTimeline = self._actionTimeline
    if not actionNode then
        actionNode = cc.CSLoader:createNode(Res.upgradeNode,false,false)
        actionTimeline = actionNode:getActionByTag(0)
        actionTimeline:setLastFrameCallFunc(function()
            actionNode:setVisible(false)
        end)
        self._ui.Panel:addChild(actionNode)
    end

    local posX, posY = self._curRender:getPositionX(), self._curRender:getPositionY()
    if self._curIsLegend then
        local panelWidth, panelHeight = self._ui.Panel_artifact:getContentSize2()
        local ap = self._ui.Panel_artifact:getAnchorPoint()
        posX = posX + self._ui.Panel_artifact:getPositionX() - ap.x * panelWidth
        posY = posY + self._ui.Panel_artifact:getPositionY() - ap.y * panelHeight
    else
        local _, height = self._curRender:getContentSize2()
        actionNode:setPosition(self._curRender:getPositionX(), self._curRender:getPositionY() + height/2)
        local panelWidth, panelHeight = self._ui.Panel_skill:getContentSize2()
        local ap = self._ui.Panel_skill:getAnchorPoint()
        posX = posX + self._ui.Panel_skill:getPositionX() - ap.x * panelWidth
        posY = posY + height/2 + self._ui.Panel_skill:getPositionY() - ap.y * panelHeight
    end
    actionNode:setPosition(posX, posY)
    actionTimeline:gotoFrameAndPlay(0, false)
end

return ClassRef
